package cn.oa.service.impl;

import cn.oa.dao.DepartmentDao;
import cn.oa.entity.Department;
import cn.oa.service.DepartmentService;
import cn.oa.util.DatabaseUtil;
import cn.oa.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class DepartmentServiceImpl implements DepartmentService {

    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    DepartmentDao departmentDao = sqlSession.getMapper(DepartmentDao.class);

    @Override
    public List<Department> getAllDepartments() throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return departmentDao.getAllDepartments();
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           MyBatisUtil.closeSqlSession(sqlSession);
        }
    }
}
